﻿using Common.DataModels;
using Extensions.Attribute.TransactionAttribute;
using Microsoft.AspNetCore.Mvc;
using PLC.Domain.Dto;
using PLC.Domain.Service.Service;

namespace PLC.API.Controllers
{
    [Route("api/device-config")]
    [ApiController]
    public class DeviceConfigController : BaseApiController
    {
        private readonly DeviceConfigService _deviceConfigService;

        public DeviceConfigController(DeviceConfigService deviceConfigService)
        {
            _deviceConfigService = deviceConfigService?? throw new ArgumentNullException(nameof(deviceConfigService));
        }
        /// <summary>
        /// 精确查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<DtoDeviceConfig>> FindOne([FromRoute] long id)
        {
            var data = await _deviceConfigService.FindOneAsync(id);
            return Success(data, "查询成功");
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pagingModel"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<DtoDeviceConfig>>> FindByPage([FromQuery] PagingModel pagingModel)
        {
            var data = await _deviceConfigService.FindByPageAsync(pagingModel);
            return Success(data.Item1,data.Item2, "查询成功");
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="dtoDeviceConfig"></param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public async Task<MessageModel<DtoDeviceConfig>> Create([FromBody] DtoDeviceConfig dtoDeviceConfig)
        {
            var data = await _deviceConfigService.CreateAsync(dtoDeviceConfig);
            return Success(data, "创建成功");
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dtoDeviceConfig"></param>
        /// <returns></returns>
        [HttpPut]
        [Transaction]
        public async Task<MessageModel<DtoDeviceConfig>> Update([FromBody] DtoDeviceConfig dtoDeviceConfig)
        {
            var data = await _deviceConfigService.UpdateAsync(dtoDeviceConfig);
            return Success(data, "修改成功");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Transaction]
        public async Task<MessageModel> Delete([FromBody] List<long> ids)
        {
            await _deviceConfigService.DeleteAsync(ids);
            return Success("删除成功");
        }

        [HttpGet("{id}/communication-test")]
        public async Task<MessageModel> CommunicationTest(long id)
        {
            var result = await _deviceConfigService.CommunicationTest(id);
            return result ? Success("通讯成功") : Fail("通讯失败");
        }
    }
}
